package top.lc951.myandroid;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

public class CustomIndicatorActivity extends AppCompatActivity {

    public static void actionActivity(Context context) {
        Intent intent = new Intent(context, CustomIndicatorActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_indicator);
        initTabViews();
    }

    private Map<Integer, TextView> tabMap = new HashMap<>();

    private void initTabViews() {
        TextView foreTv = findViewById(R.id.tv_fore_show);
        foreTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setForeTabText();
            }
        });
        TextView playbackTv = findViewById(R.id.tv_playback);
        playbackTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setPlaybackTabText();
            }
        });

        TextView tab1Tv = findViewById(R.id.tv_tab_1);
        TextView tab2Tv = findViewById(R.id.tv_tab_2);
        TextView tab3Tv = findViewById(R.id.tv_tab_3);
        TextView tab4Tv = findViewById(R.id.tv_tab_4);
        TextView tab5Tv = findViewById(R.id.tv_tab_5);
        TextView tab6Tv = findViewById(R.id.tv_tab_6);
        TextView tab7Tv = findViewById(R.id.tv_tab_7);

        tabMap.put(1, tab1Tv);
        tabMap.put(2, tab2Tv);
        tabMap.put(3, tab3Tv);
        tabMap.put(4, tab4Tv);
        tabMap.put(5, tab5Tv);
        tabMap.put(6, tab6Tv);
        tabMap.put(7, tab7Tv);

        setForeTabText();

        selectTabTv(tab1Tv);

        tab1Tv.setOnClickListener(tabStatusChangeOnClickListener);
        tab2Tv.setOnClickListener(tabStatusChangeOnClickListener);
        tab3Tv.setOnClickListener(tabStatusChangeOnClickListener);
        tab4Tv.setOnClickListener(tabStatusChangeOnClickListener);
        tab5Tv.setOnClickListener(tabStatusChangeOnClickListener);
        tab6Tv.setOnClickListener(tabStatusChangeOnClickListener);
        tab7Tv.setOnClickListener(tabStatusChangeOnClickListener);
    }

    private void setPlaybackTabText() {
        clearSelectedStatus();
        setTabCenter3Visible();
    }

    private void setTabCenter3Visible() {
        clearSelectedStatus();
        tabMap.get(1).setVisibility(View.INVISIBLE);
        tabMap.get(2).setVisibility(View.INVISIBLE);
        tabMap.get(3).setText("默认");
        tabMap.get(4).setText("最新");
        tabMap.get(5).setText("最热");
        tabMap.get(6).setVisibility(View.INVISIBLE);
        tabMap.get(7).setVisibility(View.INVISIBLE);

        selectTabTv(tabMap.get(3));
    }

    private void setForeTabText() {
        clearSelectedStatus();
        setAllTabVisible();
        tabMap.get(1).setText("今天");
        tabMap.get(2).setText("明天");
        tabMap.get(3).setText("后天");
        tabMap.get(4).setText("周六");
        tabMap.get(5).setText("周日");
        tabMap.get(6).setText("周一");
        tabMap.get(7).setText("周二");

        selectTabTv(tabMap.get(1));
    }

    private void setAllTabVisible() {
        for (int i = 1; i <= 7; i++) {
            TextView tabItemTv = tabMap.get(i);
            tabItemTv.setVisibility(View.VISIBLE);
        }
    }

    private void selectTabTv(TextView selectTabTv) {
        Drawable bottomDrawable = getResources().getDrawable(R.drawable.shape_indicator_ffdc3328_23_1);
        bottomDrawable.setBounds(0, 0
                , bottomDrawable.getMinimumWidth()
                , bottomDrawable.getMinimumHeight()
        );
        selectTabTv.setCompoundDrawables(null, null, null
                , bottomDrawable);
    }

    View.OnClickListener tabStatusChangeOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            clearSelectedStatus();
            selectTabTv((TextView) view);
        }
    };

    private void clearSelectedStatus() {
        for (int i = 1; i <= 7; i++) {
            TextView tabItemTv = tabMap.get(i);
            tabItemTv.setCompoundDrawables(null, null, null
                    , null);
        }
    }
}